package com.smxy.mall.service.impl;

import com.smxy.mall.common.CustomException;
import com.smxy.mall.common.Response;
import com.smxy.mall.dao.RoleDao;
import com.smxy.mall.entity.Role;
import com.smxy.mall.model.request.RoleReq;
import com.smxy.mall.service.RoleService;
import com.smxy.mall.utils.Common;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleDao roleDao;

    @Override
    public List<Role> list() {
        return roleDao.findAll();
    }

    @Override
    public Role save(RoleReq roleReq) {
        Role role = new Role();
        if(Common.isId(roleReq.getId()+"")){
            role.setId(roleReq.getId());
        }
        role.setRoleName(roleReq.getRoleName());
        role.setRemark(roleReq.getRemark());
        role.setStatus(roleReq.getStatus());
        role.setRole(roleReq.getRole());
        return roleDao.save(role);
    }

    @Override
    public Role changeStatus(Integer status, Integer id) {
        Role role = roleDao.findById(id);
        if(!Common.isId(role.getId()+"")){
            throw  new CustomException(Response.fail("角色不存在"));
        }
        role.setStatus(status);
        return roleDao.save(role);
    }

    @Override
    public Role roleInfo(Integer id) {
        Role role = roleDao.findById(id);
        if(!Common.isId(role.getId()+"")){
            throw  new CustomException(Response.fail("角色不存在"));
        }
        return role;
    }
}
